<?php

namespace App\Http\Controllers\Admin\Service;

use App\Http\Controllers\Controller as BaseController;
use App\Repository\Commit\DictDatarRepository;
use App\Repository\Service\ServiceRepository;
use Illuminate\Http\Request;

// 客服模块
class IndexController extends BaseController
{
    private $repoist;

    public function __construct(ServiceRepository $repository)
    {
        $this->repoist = $repository;
    }

//接单大厅
    public function index(Request $request)
    {
//         var_dump($where);
        return $this->repoist->index($request, $request->get('pageNum'), $request->get('pageSize'));
    }

//    一些配置的东西
    public function setting(Request $request)
    {
        $type = ['service_assign_type', 'service_assign_max'];
        $RepoistDict = new DictDatarRepository();
        if ($request->has('loadData') && $request->get('loadData') == 1) {

            $data = $RepoistDict->getTypeValue($type);
            $assignTypeList = config('card.serviceAssignType');
            $data = $this->returnMsg(200, '查询完毕', $data);
            $data['assignTypeList'] = $assignTypeList;
            return $data;
        }
        foreach ($type as $k => $key) {
            $value = $request->get($key);
            $RepoistDict->setTypeValue($key, $value);
        }
        return $this->returnMsg(200, '操作成功');
    }

    public function online(Request $request)
    {
        if ($request->has('loadData') && $request->get('loadData') == 1) {
            return $this->repoist->loadServiceUsers($request->user);
        }
        return $this->repoist->updateServiceUsers($request->all());
    }

//归属报表
    public function assignReport(Request $request)
    {
        return $this->repoist->loadAssignUsersReport($request->all());
    }

    //抢单通知单
    public function robNotification(Request $request)
    {
        $userId = $request->user->id;
        if (empty($userId)) {
            return $this->returnMsg(1, '请登陆后操作');
        }
        return $this->repoist->robNotification();
    }

//分配客服的时候拉客服信息
    public function assignServiceOptionsList(Request $request)
    {
        $params = $request->all();
        return $this->repoist->assignServiceOptionsList($params['user']);
    }

//    指定客服人员
    public function editAssignService(Request $request)
    {
        $serviceId = $request->get('service_id');
        if (empty($serviceId)) {
            return $this->returnMsg(1, '请登陆后操作');
        }
        $ids = $request->get('id');
        return $this->repoist->editAssignService($request->user->id, $serviceId, $ids);
    }

//    指定客服人员
    public function assign(Request $request)
    {
        $serviceId = $request->get('service_id');
        if (empty($serviceId)) {
            return $this->returnMsg(1, '请登陆后操作');
        }
        $ids = $request->get('id');
        if (empty($ids)) {
            return $this->returnMsg(1, '请指明订单');
        }
        return $this->repoist->receive($serviceId, $ids);
    }

//    随机客服人员
    public function randomAssign(Request $request)
    {
        $res = $this->assignServiceOptionsList($request);
        if (empty($res['data'])) {
            return $res;
        }
        $key = array_rand($res['data'], 1);
        $userId = $res['data'][$key]['id'];
        $ids = $request->get('id');
        return $this->repoist->receive($userId, $ids);
    }

//    自己接单操作
    public function receive(Request $request)
    {
        $userId = $request->user->id;
        if (empty($userId)) {
            return $this->returnMsg(1, '请登陆后操作');
        }
        $ids = $request->get('id');
        return $this->repoist->receive($userId, $ids);
    }

//    我的已接订单
//    public function workList(Request $request)
//    {
//        $data = $this->repoist->workList($request, $request->get('pageNum'), $request->get('pageSize'));
////        if ($data['code'] == 200) {
////            $data['searchTypeList'] = config('card.serviceWorkTypeList');
////        }
//        return $data;
//    }

//    记录订单跟进工作
    public function saveWork(Request $request)
    {
        return $this->repoist->workList($request, $request->get('pageNum'), $request->get('pageSize'));
    }
//
////    修改订单的卡的在网状态
//    public function changeIsOnline(Request $request)
//    {
//        $id = $request->get("id");
//        $value = (int)$request->get('status');
//        $userId = $request->user->id;
//        return $this->repoist->changeIsOnline($userId, $id, $value);
//    }
//
////修改卡的余额
//    public function changeCardBalance(Request $request)
//    {
//        $id = $request->get("id");
//        $value = (float)$request->get('money');
//        $userId = $request->user->id;
//        return $this->repoist->changeCardBalance($userId, $id, $value);
//    }
//
////审核订单
//    public function reviewIt(Request $request)
//    {
//        $id = $request->get("id");
//        $status = (int)$request->get('status');
//        $reason = trim($request->get('reason'));
//        $userId = $request->user->id;
//        return $this->repoist->reviewIt($userId, $id, $status, $reason);
//    }
//
////返回审核不通过的原因
//    public function reviewReasonList(Request $request)
//    {
//        $id = $request->get("id");
////        $userId = $request->user->id;
//        return $this->repoist->reviewReasonList($id);
//    }
//
////日志列表
//    public function logsList(Request $request)
//    {
//        $id = $request->get("id");
//        return $this->repoist->logsList($id);
//    }
//
//    public function add(Request $request)
//    {
////        不能用通用的增加，要重写
//        return $this->repoist->add($request);
//    }
//
//    public function edit(Request $request)
//    {
//        if (!$request->get('id')) {
//            return $this->returnMsg(1, '请传递id参数');
//        }
//        return $this->repoist->edit($request);
//    }
//
////    public function del(Request $request, $id)
////    {
////        if (!$id) {
////            return $this->returnMsg(1, '请传递参数');
////        }
////        return $this->repoist->unifiedDel($id);
////    }
//    public function changeStatus(Request $request)
//    {
//        $id = $request->get("id");
//        // 需要修改两个地方 一个是状态 一个 app是否上架
//        $status = (int)$request->get('status');
//        return $this->repoist->unifiedUpdateStatus($id, $status);
//    }
}
